package gdut.imis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import gdut.imis.Utils.Result;
import gdut.imis.domain.CollegeMajorRel;
import gdut.imis.domain.Major;
import gdut.imis.enums.Status;
import gdut.imis.mapper.MajorMapper;
import gdut.imis.service.CollegeMajorRelService;
import gdut.imis.service.MajorService;
import gdut.imis.vo.MajorVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @Description
 * @Author JiaFu
 * @Date 2023/4/8 18:16
 */
@Service
@RequiredArgsConstructor
public class MajorServiceImpl extends ServiceImpl<MajorMapper, Major> implements MajorService {

    private final MajorMapper majorMapper;
    private final CollegeMajorRelService collegeMajorRelService;

    /***
     * 新增大学专业档案(拥有major权限)
     * @param: majorVo
     * @return Result
     */
    @Override
    @Transactional
    public Result addMajor(MajorVo majorVo) {
        if (Objects.isNull(majorVo)) {
            return Result.errorWithArgs(Status.REQUEST_PARAMS_NOT_VALID_ERROR, "majorVo");
        }
        Major major = new Major();
        BeanUtils.copyProperties(majorVo, major);
        //判断该专业是否已被录入
        LambdaQueryWrapper<Major> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Major::getUniversityId, major.getUniversityId());
        queryWrapper.eq(Major::getName, major.getName());
        Major one = this.getOne(queryWrapper);
        if (one != null) {
            return Result.errorWithArgs(Status.FAILED, major.getName() + "专业档案已存在");
        }
        //不存在可新增
        boolean flag = this.save(major);
        if (flag) {
            CollegeMajorRel collegeMajorRel = new CollegeMajorRel();
            collegeMajorRel.setCollegeId(majorVo.getCollegeId());
            collegeMajorRel.setMajorId(major.getId());
            boolean flag2 = collegeMajorRelService.save(collegeMajorRel);
            if (flag2) {
                return Result.success();
            }
            return Result.errorWithArgs(Status.FAILED, "录入专业档案失败");
        }
        return Result.errorWithArgs(Status.FAILED, "录入专业档案失败");
    }

    /***
     * 获取专业列表
     * @return Result
     */
    @Override
    public Result getMajorList(Long universityId) {
        LambdaQueryWrapper<Major> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Major::getUniversityId, universityId);
        List<Major> majorList = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(majorList)) {
            return Result.errorWithArgs(Status.FAILED, "没有查询到任何专业信息");
        }
        return Result.success(majorList);
    }

    /***
     * 根据学院id获取专业列表
     * @param: collegeId
     * @return Result
     */
    @Override
    public Result getMajorListByCollegeId(Long collegeId) {
        List<Major> majorList = majorMapper.getMajorListByCollegeId(collegeId);
        if (CollectionUtils.isEmpty(majorList)) {
            return Result.errorWithArgs(Status.FAILED,"没有查询到任何专业信息");
        }
        return Result.success(majorList);
    }
}
